﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageRole : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_ROLE = "__ROLE::ROLE";
    private const string PROP_LAYOUT = "__ROLE::UIL";
    private const string PROP_SELECTED_ROLE_ID = "__ROLE::SROLEID";

    #endregion

    #region Public Properties

    public RolesCollection DataSourceRoles
    {
        get { return (SessionMgr.Session[PROP_DATASRC_ROLE] is RolesCollection ? (RolesCollection)SessionMgr.Session[PROP_DATASRC_ROLE] : (RolesCollection)(SessionMgr.Session[PROP_DATASRC_ROLE] = new RolesCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_ROLE] = value; }
    }

    public int SelectedROLEID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_ROLE_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_ROLE_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_ROLE_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceRoles = Role.GetAllRoles();
            RolesList1.DataBind(DataSourceRoles);
            divexistname.Visible = false;
        }
    }

    #endregion

    #region Control: RoleList

    protected void ucRoleList_EditRequested(object sender, Controls_RolesList.EditRequestedEventArgs evt)
    {
        SelectedROLEID = evt.RL.LU_RoleID;
        RolesViewEdit1.DataBind(evt.RL);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucRolesList_AddNewRequested(object sender, EventArgs evt)
    {
        RolesViewEdit1.CancelEdit();
        Role newRole = Role.CreateNew(DataSourceRoles);
        SelectedROLEID = newRole.LU_RoleID;
        RolesViewEdit1.DataBind(DataSourceRoles.FindByRoleID(SelectedROLEID));
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucRoleList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceRoles = Role.GetAllRoles();
        RolesList1.DataSource = DataSourceRoles;
    }

    #endregion

    #region Control: FAQViewEdit

    protected void ucRoleViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        RolesViewEdit1.DataSourceRole = DataSourceRoles.FindByRoleID(SelectedROLEID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        RolesViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceRoles.FindByRoleID(SelectedROLEID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceRoles.Remove(DataSourceRoles.FindByRoleID(SelectedROLEID));
                SelectedROLEID = (DataSourceRoles[0] != null ? DataSourceRoles[0].LU_RoleID : 0);
            }
            catch
            {
            }
        }
        RolesList1.DataBind(DataSourceRoles);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (RolesViewEdit1.EndEdit())
        {
            Role obj = DataSourceRoles.FindByRoleID(SelectedROLEID);
            Role.Update(obj);
            //Set the feedback text here.....
            DataSourceRoles = Role.GetAllRoles();
            RolesList1.DataBind(DataSourceRoles);
            SetLayout(LayoutType.View);
            Response.AppendHeader("Refresh", "0");
            divexistname.Visible = false;
        }
        else
        {
            divexistname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlRoleList.Visible = false;
                pnlRoleViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlRoleList.Visible = true;
                pnlRoleViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
